package com.hmyd.hmyd_android.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;

import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.adapter.AbstractWheelTextAdapter;
import com.hmyd.hmyd_android.weigets.OnWheelChangedListener;
import com.hmyd.hmyd_android.weigets.OnWheelScrollListener;


public class WheelCityPopupwindow extends PopupWindow implements OnClickListener{
	// Scrolling flag
	private boolean scrolling = false;
	
	private View mainView;
	private View cancel;
	private View ok;
	
	private Activity context;
	private WheelPopupListener mWheelPopupListener;
	private WheelView country;
	
	private String mCountries[];
	
	public WheelCityPopupwindow(Activity paramActivity, WheelPopupListener wheelPopupListener, int paramInt1, int paramInt2, String countries[]){  
		super(paramActivity);  
		
		context = paramActivity;
		
		mainView = LayoutInflater.from(paramActivity).inflate(R.layout.wheel_city_popup_window, null);  
		
		mCountries = countries;
		initViews(mainView);
		
		//设置每个子布局的事件监听器  
		mWheelPopupListener = wheelPopupListener;
		
		setContentView(mainView);  
		
		this.setWidth(LayoutParams.MATCH_PARENT);
		this.setHeight(LayoutParams.WRAP_CONTENT);
		
		//设置宽度  
		/*setWidth(paramInt1);  
		//设置高度  
		setHeight(paramInt2); */ 
		//设置显示隐藏动画  
		setAnimationStyle(R.style.AnimTools);  
		//设置背景透明  
		setBackgroundDrawable(new ColorDrawable(0));  
   }
	
	private void initViews(View root) {
		country = (WheelView) root.findViewById(R.id.country);
		country.setVisibleItems(3);
		country.setViewAdapter(new CountryAdapter(context));

		country.addChangingListener(new OnWheelChangedListener() {
			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				if (!scrolling) {
					
				}
			}
		});

		country.addScrollingListener( new OnWheelScrollListener() {
			@Override
			public void onScrollingStarted(WheelView wheel) {
				scrolling = true;
			}
			@Override
			public void onScrollingFinished(WheelView wheel) {
				scrolling = false;
			}
		});

		country.setCurrentItem(0);
		
		cancel = mainView.findViewById(R.id.cancel);
		ok = mainView.findViewById(R.id.ok);
		
		cancel.setOnClickListener(this);  
		ok.setOnClickListener(this);  
	}
	
	/**
	 * Adapter for countries
	 */
	private class CountryAdapter extends AbstractWheelTextAdapter {
		/**
		 * Constructor
		 */
		protected CountryAdapter(Context context) {
			super(context, R.layout.view_wheel_country_layout, NO_RESOURCE);

			setItemTextResource(R.id.country_name);
		}

		@Override
		public View getItem(int index, View cachedView, ViewGroup parent) {
			View view = super.getItem(index, cachedView, parent);
//			ImageView img = (ImageView) view.findViewById(R.id.flag);
//			img.setImageResource(flags[index]);
			return view;
		}

		@Override
		public int getItemsCount() {
			return mCountries.length;
		}

		@Override
		protected CharSequence getItemText(int index) {
			return mCountries[index];
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.cancel:
			dismiss();
			break;
		case R.id.ok:
			mWheelPopupListener.getFirstString(country.getCurrentItem());
			dismiss();
			break;
		default:
			break;
		}
	}
}
